﻿using Microsoft.Win32;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xbd.s7netplus;

namespace lyh.FG2JiaYaZhanDemo
{
    public partial class FrmParamSet : Form
    {
        private SysInfo sysInfo;
        private SysInfoServer sysInfoServer;
        private string sysInfoPath;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysInfo"></param>
        /// <param name="sysInfoServer"></param>
        /// <param name="sysInfoPath"></param>
        public FrmParamSet(SysInfo sysInfo, SysInfoServer sysInfoServer, string sysInfoPath)
        {
            InitializeComponent();
            this.sysInfo = sysInfo;
            this.sysInfoServer = sysInfoServer;
            this.sysInfoPath = sysInfoPath;
            Load += FrmParamSet_Load;
        }

        #region 无边框拖动 
        private Point mPoint;      
        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }
        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }
        #endregion

        private void FrmParamSet_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            this.comb_cpu.DataSource = Enum.GetNames(typeof(CpuType));
            if (sysInfo != null)
            { 
                this.text_ip .Text = this.sysInfo.IPAddress;
                this.comb_cpu.Text =this.sysInfo.CpuType.ToString();
                this.text_rock.Text = this.sysInfo.Rack.ToString();
                this.text_slot.Text = this.sysInfo.Slot .ToString();

                this.uiSt_ziqidong.Active= this.sysInfo.AutoStart;
                this.text_xptime.Text = this.sysInfo.ScreenTime.ToString();
                this.text_zxtime.Text = this.sysInfo.LogoffTime.ToString();
               
            }
            this.uiSt_ziqidong.ValueChanged += uiSt_ziqidong_ValueChanged; 
        }

        /// <summary>
        /// 保存通信参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiBt_txwc_Click(object sender, EventArgs e)
        {
            if (this.sysInfo == null)
            { 
                sysInfo = new SysInfo();
            }

            this.sysInfo.IPAddress = this.text_ip.Text.Trim();
            this.sysInfo.CpuType = (CpuType)Enum.Parse(typeof(CpuType), this.comb_cpu.Text.Trim(),true);
            this.sysInfo.Rack =short.Parse(this.text_rock.Text);
            this.sysInfo.Slot = short.Parse(this.text_slot.Text);
            bool result = sysInfoServer.SetSysInfoToPath(sysInfo, sysInfoPath);
            if (result)
            { 
                this.DialogResult=DialogResult.OK;//是用于设置对话框的结果为“确定”（OK）。这通常在用户成功完成某个操作（例如，成功保存通信参数）后使用，以便调用该对话框的代码可以知道操作已经成功完成，并可以相应地进行后续处理。
                UIMessageTip.Show("保存通信参数成功！");
            }
            else
            {
                MessageBox.Show("保存通信参数失败！");   
            }
            
        }

        /// <summary>
        /// 取消通信参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiBt_txqx_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 保存参数设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiBt_cswc_Click(object sender, EventArgs e)
        {
            if (this.sysInfo == null)
            {
                sysInfo = new SysInfo();
            }

            this.sysInfo.AutoStart = this.uiSt_ziqidong.Active;
            this.sysInfo.ScreenTime = Convert.ToInt32(this.text_xptime.Text.Trim());
            this.sysInfo.LogoffTime = Convert.ToInt32(this.text_zxtime.Text.Trim());
            this.sysInfo.CameraIndex = this.comb_cm.SelectedIndex;
            bool result = sysInfoServer.SetSysInfoToPath(sysInfo, sysInfoPath);
            if (result)
            {
                this.DialogResult = DialogResult.OK;//是用于设置对话框的结果为“确定”（OK）。这通常在用户成功完成某个操作（例如，成功保存通信参数）后使用，以便调用该对话框的代码可以知道操作已经成功完成，并可以相应地进行后续处理。
                UIMessageTip.Show("保存通信参数成功！");
            }
            else
            {
                MessageBox.Show("保存通信参数失败！");
            }
        }

        /// <summary>
        /// 取消参数设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiBt_csqx_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void label3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 开机启动
        /// <summary>  
        /// 修改程序在注册表中的键值  
        /// </summary>  
        /// <param name="isAuto">true:开机启动,false:不开机自启</param> 
        private void AutoStart(bool isAuto = true)
        {
            if (isAuto == true)
            {
                RegistryKey R_local = Registry.CurrentUser;
                RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                R_run.SetValue("FGPressurizationStationPro", System.Windows.Forms.Application.ExecutablePath);
                R_run.Close();
                R_local.Close();
            }
            else
            {
                RegistryKey R_local = Registry.CurrentUser;
                RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                R_run.DeleteValue("FGPressurizationStationPro", false);
                R_run.Close();
                R_local.Close();
            }
        }
        #endregion

        private void uiSt_ziqidong_ValueChanged(object sender, bool value)
        {
            AutoStart(this.uiSt_ziqidong.Active);
        }
    }
}

